package utils;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;

public final class JSONResponse {
	private static Logger logger = LogManager.getLogger(JSONResponse.class);

	public static void returnJsonToResponse(String jsonData) {
		JSONResponse.returnJson(jsonData);
	}

	public static void returnJsonToResponse(JSONObject jsonData) {
		JSONResponse.returnJson(jsonData);
	}

	private static void returnJson(Object jsonData) {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("paragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			logger.error(Pub.STACK_TRACE, e);
		}
		out.print(jsonData);
		out.flush();
		out.close();
	}
}
